/*
 * Screenshot.java (requires Java 1.4+)
 */

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.Socket;

import javax.imageio.*;

public class Screenshot {
	public static void main(String[] args) throws Exception {
		// make sure we have exactly two arguments, 
		// a waiting period and a file name
//		if (args.length != 2) {
//			System.err.println("Usage: java Screenshot " +
//				"WAITSECONDS OUTFILE.png");
//			System.exit(1);
//		}
		// check if file name is valid
//		String[] blas = ImageIO.getWriterFormatNames();
//		for (int i = 0; i < blas.length; i++){
//			System.out.println(blas[i]);
//		}
		Screenshot screen = new Screenshot();
		
		String servidor =  "192.168.123.132";
		int puerto = 8001;
		
		Socket socket = new Socket(servidor,puerto);
		for (int i =0; i < 3; i++){
			//screen.enviarImagen(screen.tomaImagen(new Robot()),socket);
			screen.tomaImagen(new Robot());
		}
		socket.shutdownInput();
		socket.shutdownOutput();
		socket.close();
	}
	
	public BufferedImage tomaImagen(Robot robot) {
		
		// determine current screen size
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		Rectangle screenRect = new Rectangle(screenSize);
		
		
		// create screen shot
		
		try{
			float scale = 1f;
			BufferedImage image = robot.createScreenCapture(screenRect);
			Image image2 = image.getScaledInstance((int)(image.getWidth()*scale), 
								(int)(image.getHeight()*scale), Image.SCALE_FAST);
			
			BufferedImage iBufdImg = new BufferedImage(image2.getWidth(null),image2.getHeight(null),BufferedImage.TYPE_INT_RGB);
		    Graphics2D iBiContext = iBufdImg.createGraphics();
		    iBiContext.drawImage(image2,0,0,null);
		    
			return iBufdImg;
			
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return null;
	
	}
	
	
	public void enviarImagen(BufferedImage cosa,Socket socket){
		
		
		try{
			String extension = "png";
			//BufferedReader local = new BufferedReader(new InputStreamReader(System.in));
			//Socket socket = new Socket(servidor,puerto);
			//System.out.println("Voy a mandar" + cosa.toString());
			ImageIO.write(cosa,extension, socket.getOutputStream());
			//System.out.println("Ya mande");
			//BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			//PrintWriter outputStream = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
//			String userInput;
//			System.out.println(inputStream.readLine());
//			while ((userInput = local.readLine()) != null) {
//				outputStream.println(userInput);
//				System.out.println(inputStream.readLine());
//			}
			//socket.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
	
}